<template>
  <div class="container">
    <!-- 移动端菜单切换按钮 -->
    <div class="mobile-menu-toggle md:hidden">
      <el-button 
        type="primary" 
        circle 
        size="small"
        class="menu-toggle-btn"
        @click="toggleMobileMenu"
      >
        <el-icon><Menu /></el-icon>
      </el-button>
    </div>

    <!-- 头部 ：包含标题、添加/导出按钮 -->
    <div class="flex flex-wrap items-center justify-between mb-6 gap-3">
      <!-- 标题 -->
      <div>
        <h1 class="page-title text-2xl font-bold">绑定关联</h1>
        <p class="text-gray-500">管理车辆、驾驶员、加油卡和ETC卡之间的关联关系</p>
      </div>
     <!-- 添加/导出按钮 -->
      <div class="flex gap-2">
        <el-button type="primary" @click="openDialog()" class="btn-with-icon">
          <el-icon class="mr-1"><Plus /></el-icon>添加绑定
        </el-button>
        <el-button type="success" @click="exportData" class="btn-with-icon">
          <el-icon class="mr-1"><Download /></el-icon>导出数据
        </el-button>
      </div>
    </div>
    <!-- 搜索区域框 -->
    <el-card shadow="hover" class="mb-6 card-with-hover">
      <div class="search-container">
        <!-- 车牌号搜索 -->
        <div class="search-item">
          <el-input
            v-model="searchForm.plateNumber"
            placeholder="请输入车牌号"
            class="search-input"
            clearable
          >
            <template #prefix>
              <el-icon class="search-icon"><Van /></el-icon>
            </template>
          </el-input>
        </div>
        
        <!-- 驾驶员搜索 -->
        <div class="search-item">
          <el-input
            v-model="searchForm.driverName"
            placeholder="请输入驾驶员姓名"
            class="search-input"
            clearable
          >
            <template #prefix>
              <el-icon class="search-icon"><User /></el-icon>
            </template>
          </el-input>
        </div>      
        
        <!-- 加油卡号搜索 -->
        <div class="search-item">
          <el-input
            v-model="searchForm.fuelCardNumber"
            placeholder="请输入加油卡号"
            class="search-input"
            clearable
          >
            <template #prefix>
              <el-icon class="search-icon"><CreditCard /></el-icon>
            </template>
          </el-input>
        </div>
        
        <!-- ETC卡号搜索 -->
        <div class="search-item">
          <el-input
            v-model="searchForm.etcCardNumber"
            placeholder="请输入ETC卡号"
            class="search-input"
            clearable
          >
            <template #prefix>
              <el-icon class="search-icon"><CreditCard /></el-icon>
            </template>
          </el-input>
        </div>
        
        <!-- 操作按钮 -->
        <div class="search-actions">
          <el-button type="primary" @click="handleSearch" class="search-btn">
            <el-icon class="mr-1"><Search /></el-icon>查询
          </el-button>
          
          <el-button @click="resetSearch" class="reset-btn" type="info" plain>
          <el-icon class="mr-1"><Refresh /></el-icon>重置
        </el-button>
        </div>
      </div>
    </el-card>
    
    <el-card shadow="hover" class="overflow-hidden card-with-hover">
    
      <el-table   
        v-loading="loading"  
        :data="filteredBindings"
        border
        stripe
        style="width: 100%"
        :header-cell-class-name="'bg-gray-100 dark:bg-gray-700'"
        class="custom-table"
        fit
        @sort-change="handleSortChange"
      >
        <!-- 始终显示的列 -->
        <el-table-column prop="id" label="ID" width="80" align="center" sortable="custom" fixed="left" />
        <el-table-column prop="plateNumber" label="车牌号" min-width="150" sortable="custom" fixed="left" />
        
        <!-- 在中等及以上屏幕显示的列 -->
        <el-table-column prop="driverName" label="驾驶员" min-width="150" sortable="custom" class-name="hide-on-mobile" />
        <el-table-column prop="fuelCardNumber" label="加油卡号" min-width="200" show-overflow-tooltip sortable="custom" class-name="hide-on-mobile" />
        <el-table-column prop="etcCardNumber" label="ETC卡号" min-width="200" show-overflow-tooltip sortable="custom" class-name="hide-on-mobile" />
        <el-table-column prop="bindTime" label="绑定时间" min-width="200" sortable="custom" class-name="hide-on-mobile">
          <template #default="{ row }">
            {{ row.bindTime || '-' }}
          </template>
        </el-table-column>

        <!-- 操作列始终显示 -->
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <div class="action-buttons">
               <!-- 编辑按钮 -->
              <el-button 
                type="primary" 
                size="small"
                circle
                @click="editBinding(scope.row)"
                class="edit-btn"
              >
                <el-icon><Edit /></el-icon>
              </el-button>
                <!-- 删除按钮 -->
              <el-button 
                type="danger" 
                size="small"
                circle
                @click="deleteBinding(scope.row)"
                class="delete-btn"
              >
                <el-icon><Delete /></el-icon>
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container mt-4 flex justify-center">
        <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="DEFAULT_PAGE_SIZES"       
          :total="totalBindings"                
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"      
          @current-change="handleCurrentChange"   
          background                           
          class="custom-pagination"           
        />
      </div>
    </el-card>
    
    <!-- 添加/编辑绑定对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="isEdit ? '编辑绑定' : '添加绑定'"
      width="500px"
      destroy-on-close
      class="custom-dialog"
    >
      <el-form
        ref="formRef" 
        :model="bindingForm" 
        :rules="rules" 
        label-width="100px"
        label-position="right"
        class="form-container"
      >
        <el-form-item label="车牌号" prop="plateNumber">
          <el-select v-model="bindingForm.plateNumber" placeholder="请选择车辆" filterable style="width: 100%" @change="handleVehicleChange">
            <el-option 
              v-for="vehicle in vehicles" 
              :key="vehicle.id" 
              :label="vehicle.plateNumber" 
              :value="vehicle.plateNumber" 
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驾驶员" prop="driverName">
          <el-select v-model="bindingForm.driverName" placeholder="请选择驾驶员" filterable style="width: 100%" @change="handleDriverChange">
            <el-option 
              v-for="driver in drivers" 
              :key="driver.id" 
              :label="driver.name" 
              :value="driver.name" 
            />
          </el-select>
        </el-form-item>
        <el-form-item label="加油卡" prop="fuelCardNumber">
          <el-select v-model="bindingForm.fuelCardNumber" placeholder="请选择加油卡" filterable style="width: 100%" @change="handleFuelCardChange">
            <el-option 
              v-for="card in fuelCards" 
              :key="card.id" 
              :label="card.cardNumber" 
              :value="card.cardNumber" 
            />
          </el-select>
        </el-form-item>
        <el-form-item label="ETC卡" prop="etcCardNumber">
          <el-select v-model="bindingForm.etcCardNumber" placeholder="请选择ETC卡" filterable style="width: 100%" @change="handleEtcCardChange">
            <el-option 
              v-for="card in etcCards" 
              :key="card.id" 
              :label="card.cardNumber" 
              :value="card.cardNumber" 
            />
          </el-select>
        </el-form-item>
        <el-form-item label="绑定时间" prop="bindTime">
          <el-date-picker
            v-model="bindingForm.bindTime"
            type="datetime"
            placeholder="选择绑定时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm" :loading="submitLoading">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Download, Search, Refresh, Edit, Delete, Menu, Van, User, CreditCard } from '@element-plus/icons-vue'
import { bindingApi, vehicleApi, driverApi, fuelCardApi, etcCardApi } from '@/api'

export default {
  name: 'Bindings',
  components: {
    Search,
    Plus,
    Download,
    Edit,
    Delete,
    Refresh,
    Menu,
    Van,
    User,
    CreditCard
  },
  setup() {
    // 常量
    const DEFAULT_PAGE_SIZES = [10, 20, 50, 100]

    // 表单相关
    const formRef = ref(null)
    const dialogVisible = ref(false)
    const isEdit = ref(false)
    const bindingForm = reactive({
      id: null,
      vehicleId: null,
      plateNumber: '',
      driverId: null,
      driverName: '',
      fuelCardId: null,
      fuelCardNumber: '',
      etcCardId: null,
      etcCardNumber: '',
      bindTime: new Date().toISOString().split('T')[0],
      remark: ''
    })

    // 搜索相关
    const searchForm = reactive({
      plateNumber: '',
      driverName: '',
      fuelCardNumber: '',
      etcCardNumber: ''
    })

    // 表格相关
    const loading = ref(false)
    const bindings = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const totalBindings = ref(0)
    const sortBy = ref('id')
    const sortOrder = ref('descending')

    // 可选项数据
    const vehicles = ref([])
    const drivers = ref([])
    const fuelCards = ref([])
    const etcCards = ref([])

    // 验证规则
    const rules = {
      plateNumber: [
        { required: true, message: '请选择车牌号', trigger: 'change' }
      ],
      driverName: [
        { required: true, message: '请选择驾驶员', trigger: 'change' }
      ],
      fuelCardNumber: [
        { required: false, message: '请选择加油卡号', trigger: 'change' }
      ],
      etcCardNumber: [
        { required: false, message: '请选择ETC卡号', trigger: 'change' }
      ],
      bindTime: [
        { required: true, message: '请选择绑定时间', trigger: 'change' }
      ]
    }

    // 计算属性
    const filteredBindings = computed(() => {
      return bindings.value.map(binding => {
        // 处理绑定时间
        if (!binding.bindTime || binding.bindTime === 'undefined') {
          binding.bindTime = '';
        }
        
        // 确保所有字段都有值
        if (!binding.plateNumber) binding.plateNumber = '-';
        if (!binding.driverName) binding.driverName = '-';
        if (!binding.fuelCardNumber) binding.fuelCardNumber = '-';
        if (!binding.etcCardNumber) binding.etcCardNumber = '-';
        
        return binding;
      });
    })

    // 方法
    const loadBindings = async () => {
      loading.value = true
      try {
        // 构造API请求参数
        const params = {
          current: currentPage.value,
          size: pageSize.value,
          plateNumber: searchForm.plateNumber || null,
          driverName: searchForm.driverName || null,
          fuelCardNumber: searchForm.fuelCardNumber || null,
          etcCardNumber: searchForm.etcCardNumber || null
        }
        
        // 调用bindingApi的getBindingList方法
        const page = await bindingApi.getBindingList(params)
        
        // 解析返回的数据
        if (page && page.records) {
          // 处理每条记录的数据格式
          const processedRecords = page.records.map(binding => {
            // 处理绑定时间
            if (!binding.bindTime) {
              binding.bindTime = '';
            } else if (binding.bindTime.includes('T')) {
              // 如果是ISO格式的日期，只保留日期部分
              binding.bindTime = binding.bindTime.split('T')[0];
            }
            
            // 确保所有字段都有值
            if (!binding.plateNumber) binding.plateNumber = '-';
            if (!binding.driverName) binding.driverName = '-';
            if (!binding.fuelCardNumber) binding.fuelCardNumber = '-';
            if (!binding.etcCardNumber) binding.etcCardNumber = '-';
            
            return binding;
          });
          
          bindings.value = processedRecords;
          totalBindings.value = page.total
          console.log('绑定数据加载成功:', bindings.value.length, '条记录')
        } else {
          bindings.value = []
          totalBindings.value = 0
          console.warn('返回的绑定数据格式不正确:', page)
        }
      } catch (error) {
        console.error('加载绑定数据失败:', error)
        ElMessage.error('加载数据失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }

    // 加载可选车辆
    const loadAvailableVehicles = async () => {
      try {
        const result = await vehicleApi.getAvailableVehicles()
        if (Array.isArray(result)) {
          vehicles.value = result
        } else {
          vehicles.value = []
          console.warn('加载可用车辆返回的数据格式不正确:', result)
        }
      } catch (error) {
        console.error('加载可用车辆失败:', error)
        ElMessage.warning('加载可用车辆失败，请稍后重试')
      }
    }

    // 加载可选驾驶员
    const loadAvailableDrivers = async () => {
      try {
        const params = { status: '1', size: 1000 } // 只加载在职驾驶员
        const result = await driverApi.getDriverList(params)
        if (result && result.records) {
          drivers.value = result.records
        } else {
          drivers.value = []
          console.warn('加载可用驾驶员返回的数据格式不正确:', result)
        }
      } catch (error) {
        console.error('加载可用驾驶员失败:', error)
        ElMessage.warning('加载可用驾驶员失败，请稍后重试')
      }
    }

    // 加载可选加油卡
    const loadAvailableFuelCards = async () => {
      try {
        const params = { status: 1, size: 1000 } // 只加载正常状态的加油卡
        const result = await fuelCardApi.getFuelCardList(params)
        if (result && result.records) {
          fuelCards.value = result.records
        } else {
          fuelCards.value = []
          console.warn('加载可用加油卡返回的数据格式不正确:', result)
        }
      } catch (error) {
        console.error('加载可用加油卡失败:', error)
        ElMessage.warning('加载可用加油卡失败，请稍后重试')
      }
    }

    // 加载可选ETC卡
    const loadAvailableEtcCards = async () => {
      try {
        const params = { status: 1, size: 1000 } // 只加载正常状态的ETC卡
        const result = await etcCardApi.getEtcCardList(params)
        if (result && result.records) {
          etcCards.value = result.records
        } else {
          etcCards.value = []
          console.warn('加载可用ETC卡返回的数据格式不正确:', result)
        }
      } catch (error) {
        console.error('加载可用ETC卡失败:', error)
        ElMessage.warning('加载可用ETC卡失败，请稍后重试')
      }
    }

    const handleSearch = () => {
      currentPage.value = 1
      loadBindings()
    }

    const resetSearch = () => {
      Object.keys(searchForm).forEach(key => {
        searchForm[key] = ''
      })
      handleSearch()
    }

    const handleSortChange = ({ prop, order }) => {
      if (prop) {
        sortBy.value = prop
        sortOrder.value = order
        loadBindings()
      }
    }

    const handleSizeChange = (size) => {
      pageSize.value = size
      loadBindings()
    }

    const handleCurrentChange = (page) => {
      currentPage.value = page
      loadBindings()
    }

    const openDialog = (isEditMode = false) => {
      isEdit.value = isEditMode
      dialogVisible.value = true
      
      if (!isEditMode) {
        // 新增模式，重置表单
        Object.keys(bindingForm).forEach(key => {
          if (key === 'bindTime') {
            bindingForm[key] = new Date().toISOString().split('T')[0]
          } else {
            bindingForm[key] = key.endsWith('Id') ? null : ''
          }
        })
        
        // 加载可选项数据
        loadAvailableVehicles()
        loadAvailableDrivers()
        loadAvailableFuelCards()
        loadAvailableEtcCards()
      }
    }

    const editBinding = (row) => {
      // 编辑模式，复制数据到表单
      Object.keys(bindingForm).forEach(key => {
        if (row[key] !== undefined) {
          bindingForm[key] = row[key]
        }
      })
      
      // 加载可选项数据
      loadAvailableVehicles()
      loadAvailableDrivers()
      loadAvailableFuelCards()
      loadAvailableEtcCards()
      
      openDialog(true)
    }

    const deleteBinding = (row) => {
      ElMessageBox.confirm(
        `确认解除车辆 ${row.plateNumber} 与驾驶员 ${row.driverName} 的绑定吗？`,
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(async () => {
        try {
          loading.value = true
          // 调用bindingApi的deleteBinding方法
          await bindingApi.deleteBinding(row.id)
          ElMessage.success('解绑成功')
          loadBindings() // 重新加载列表
        } catch (error) {
          console.error('解绑失败:', error)
          ElMessage.error('解绑失败，请稍后重试')
        } finally {
          loading.value = false
        }
      }).catch(() => {
        // 用户取消删除
      })
    }

    // 当选择车辆时，检查是否已绑定
    const handleVehicleChange = async (plateNumber) => {
      if (!plateNumber || isEdit.value) return
      
      try {
        const vehicle = vehicles.value.find(v => v.plateNumber === plateNumber)
        if (vehicle) {
          bindingForm.vehicleId = vehicle.id
          
          // 检查该车辆是否已绑定
          const result = await bindingApi.checkVehicleBinding(vehicle.id, bindingForm.id || 0)
          if (result && result.isBound) {
            ElMessage.warning(`该车辆已被绑定，请选择其他车辆`)
            bindingForm.plateNumber = ''
            bindingForm.vehicleId = null
          }
        }
      } catch (error) {
        console.error('检查车辆绑定状态失败:', error)
      }
    }

    // 当选择驾驶员时
    const handleDriverChange = (driverName) => {
      const driver = drivers.value.find(d => d.name === driverName)
      if (driver) {
        bindingForm.driverId = driver.id
      }
    }

    // 当选择加油卡时
    const handleFuelCardChange = (cardNumber) => {
      const card = fuelCards.value.find(c => c.cardNumber === cardNumber)
      if (card) {
        bindingForm.fuelCardId = card.id
      }
    }

    // 当选择ETC卡时
    const handleEtcCardChange = (cardNumber) => {
      const card = etcCards.value.find(c => c.cardNumber === cardNumber)
      if (card) {
        bindingForm.etcCardId = card.id
      }
    }

    const submitForm = () => {
      formRef.value.validate(async (valid) => {
        if (valid) {
          try {
            loading.value = true
            if (isEdit.value) {
              // 编辑模式，调用updateBinding方法
              await bindingApi.updateBinding(bindingForm)
              ElMessage.success('更新成功')
            } else {
              // 新增模式，调用addBinding方法
              await bindingApi.addBinding(bindingForm)
              ElMessage.success('添加成功')
            }
            dialogVisible.value = false
            loadBindings() // 重新加载列表
          } catch (error) {
            console.error('提交表单失败:', error)
            ElMessage.error('操作失败，请稍后重试')
          } finally {
            loading.value = false
          }
        }
      })
    }

    const exportData = async () => {
      try {
        loading.value = true
        ElMessage.info('正在导出数据，请稍候...')
        // 这里可以添加导出功能，例如调用后端导出API
        
        // 暂时使用模拟导出
        setTimeout(() => {
          ElMessage.success('数据导出成功')
          loading.value = false
        }, 1500)
      } catch (error) {
        console.error('导出数据失败:', error)
        ElMessage.error('导出失败，请稍后重试')
        loading.value = false
      }
    }

    const toggleMobileMenu = () => {
      // 控制移动端菜单显示
      const event = new CustomEvent('toggle-sidebar')
      window.dispatchEvent(event)
    }

    // 初始化
    onMounted(() => {
      loadBindings()
    })

    return {
      DEFAULT_PAGE_SIZES,
      formRef,
      dialogVisible,
      isEdit,
      bindingForm,
      searchForm,
      loading,
      bindings,
      currentPage,
      pageSize,
      totalBindings,
      filteredBindings,
      vehicles,
      drivers,
      fuelCards,
      etcCards,
      rules,
      loadBindings,
      handleSearch,
      resetSearch,
      handleSortChange,
      handleSizeChange,
      handleCurrentChange,
      openDialog,
      editBinding,
      deleteBinding,
      handleVehicleChange,
      handleDriverChange,
      handleFuelCardChange,
      handleEtcCardChange,
      submitForm,
      toggleMobileMenu,
      exportData
    }
  }
}
</script>

<style scoped>
/* 移动端菜单切换按钮样式 */
.mobile-menu-toggle {
  position: fixed;
  top: 15px;
  left: 15px;
  z-index: 1000;
}

.menu-toggle-btn {
  width: 40px;
  height: 40px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.menu-toggle-btn:hover {
  transform: scale(1.05);
}

/* 响应式布局调整 */
@media (max-width: 768px) {
  .container {
    padding: 10px;
    padding-top: 60px; /* 为固定的菜单按钮留出空间 */
}

.page-title {
    font-size: 1.5rem;
  }
  
  .btn-with-icon {
    padding: 8px 12px;
  }
  
  .search-container {
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
  }
  
  .search-item {
    width: 100%;
  }
  
  .search-actions {
    width: 100%;
    margin-left: 0;
    justify-content: space-between;
  }
  
  .search-btn, .reset-btn {
    flex: 1;
  }
  
  /* 表格标题和内容调整 */
  .custom-table :deep(th),
  .custom-table :deep(td) {
    padding: 8px 6px !important;
}
}

/* 表格在移动端的响应式处理 */
@media (max-width: 500px) {
  .custom-table :deep(.el-table__header),
  .custom-table :deep(.el-table__body),
  .custom-table :deep(.el-table__row) {
    font-size: 12px;
  }
  
  .action-buttons {
    gap: 6px !important;
  }
  
  .edit-btn, .delete-btn {
    width: 28px !important;
    height: 28px !important;
  }
  
  .edit-btn .el-icon, .delete-btn .el-icon {
    font-size: 14px !important;
  }
  
  /* 使用class-name控制列显示 */
  .custom-table :deep(.hide-on-mobile) {
    display: none !important;
  }
  
  /* 确保ID列和操作列宽度合适 */
  .custom-table :deep(.el-table__cell:first-child) {
    width: 80px !important;
    min-width: 80px !important;
    max-width: 80px !important;
  }
  
  .custom-table :deep(.el-table__cell:nth-child(2)) {
    min-width: 150px !important;
  }
  
  .custom-table :deep(.el-table__cell:last-child) {
    width: 120px !important;
    min-width: 120px !important;
    max-width: 120px !important;
  }
  
  /* 让表格横向滚动，防止溢出 */
  .custom-table {
    overflow-x: auto;
    display: block;
  }
  
  /* 调整表格固定列样式 */
  .custom-table :deep(.el-table__fixed),
  .custom-table :deep(.el-table__fixed-right) {
    height: auto !important;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  
  /* 调整分页组件 */
  .custom-pagination :deep(.el-pagination__sizes) {
    display: none !important;
  }
  
  .custom-pagination :deep(.el-pagination__jump) {
    display: none !important;
}

  /* 头部布局调整 */
  .flex.items-center.justify-between {
    flex-direction: column;
    align-items: flex-start !important;
  }
  
  .flex.items-center.justify-between > div:last-child {
    margin-top: 10px;
    width: 100%;
  display: flex;
    justify-content: space-between;
  }
  
  .flex.items-center.justify-between > div:last-child .el-button {
    flex: 1;
  }
}

/* 特别针对超小屏幕的优化 */
@media (max-width: 375px) {
  .custom-table :deep(.el-table__cell:nth-child(2)) {
    min-width: 120px !important;
  }
  
  .custom-table :deep(.el-table__cell:last-child) {
    width: 100px !important;
    min-width: 100px !important;
  }
  
  .edit-btn, .delete-btn {
    width: 26px !important;
    height: 26px !important;
  }
}

.el-table :deep(.el-table__header) th { /* 表示表格的表头字体加粗 */
  font-weight: 600;   /*600表示字体加粗*/
}

.el-card {   /* 表示卡片样式 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);  /* 表示卡片的过渡效果为0.3秒 */
  border-radius: 8px;  /* 表示卡片的圆角为8px */
}

  .card-with-hover {  /* 表示卡片悬停样式 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);  /* 表示卡片的过渡效果为0.3秒 */
}

.card-with-hover:hover {  /* 表示卡片悬停样式 */
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);  /* 表示卡片在鼠标悬停时，会有一个阴影效果 */
  transform: translateY(-3px);  /* 表示卡片在鼠标悬停时，会向下移动3px */
}

.page-title {  /* 表示页面标题样式 */
  position: relative;  /* 表示页面标题的定位方式为相对定位 */
  margin-bottom: 4px;  /* 表示页面标题的底部外边距为4px */
}

.page-title::after {  /* 表示页面标题的伪元素样式 */
  content: '';  /* 表示页面标题的伪元素内容为空 */
  position: absolute;  /* 表示页面标题的定位方式为绝对定位 */
  bottom: -4px;  /* 表示页面标题的底部外边距为4px */
  left: 0;  /* 表示页面标题的左边外边距为0 */
  width: 40px;  /* 表示页面标题的宽度为40px */
  height: 3px;  /* 表示页面标题的高度为3px */
  background-color: var(--el-color-primary);  /* 表示页面标题的背景颜色为蓝色 */
  border-radius: 3px;  /* 表示页面标题的圆角为3px */
}

.btn-with-icon {  /* 表示按钮图标样式 */  
  display: flex;  /* 表示按钮图标是一个弹性容器 */
  align-items: center;  /* 表示按钮图标的子元素在垂直方向上居中对齐 */
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);  /* 表示按钮图标的过渡效果为0.3秒 */
}

.btn-with-icon:hover {  /* 表示按钮图标悬停样式 */
  transform: translateY(-1px);  /* 表示按钮图标在鼠标悬停时，会向下移动2px */
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);  /* 表示按钮图标在鼠标悬停时，会有一个阴影效果 */
}

.search-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
}

.search-item {
  flex: 1;
  min-width: 200px;
}

.search-actions {
  display: flex;
  gap: 10px;
  margin-left: auto;
}

.search-input {
  width: 100%;
}

.search-input :deep(.el-input__wrapper) {
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05), 0 0 0 1px #dcdfe6 inset;
  transition: all 0.3s;
}

.search-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1), 0 0 0 1px var(--el-color-primary) inset;
}

.search-icon {
  color: #909399;
  font-size: 16px;
  margin-right: 4px;
}

.search-btn, .reset-btn {
  white-space: nowrap;
  display: flex;
  align-items: center;
}

.search-btn {
  background-color: var(--el-color-primary);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.search-btn:hover {
  background-color: var(--el-color-primary-dark-2);
}

.reset-btn {
  background-color: #f5f7fa;
  color: #606266;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.reset-btn:hover {
  background-color: #e9e9e9;
}

/* 表格样式 */
.custom-table {
  border-radius: 8px;
  overflow: hidden;
  width: 100%;
}

.custom-table :deep(.el-table__inner-wrapper),
.custom-table :deep(.el-table__body),
.custom-table :deep(.el-table__header) {
  width: 100% !important;
}

.custom-pagination {
  margin-top: 16px;
}

/* 对话框样式 */
.custom-dialog :deep(.el-dialog__header) {
  border-bottom: 1px solid #eee;
  padding: 16px 20px;
}

.custom-dialog :deep(.el-dialog__footer) {
  border-top: 1px solid #eee;
  padding: 16px 20px;
}

/* 操作按钮容器样式 */
.action-buttons {
  height: 100%;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px; /* gap-4 */
}

/* 按钮样式 */
.edit-btn, .delete-btn {
  transition: all 0.3s;
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  border-radius: 50%;
  box-shadow: none;
}

.edit-btn .el-icon, .delete-btn .el-icon {
  font-size: 16px;
  color: #fff;
}

.edit-btn:hover {
  background-color: #409eff !important;
  box-shadow: 0 4px 16px rgba(64, 158, 255, 0.25);
}
.delete-btn:hover {
  background-color: #f56c6c !important;
  box-shadow: 0 4px 16px rgba(245, 108, 108, 0.25);
}
.edit-btn:hover .el-icon,
.delete-btn:hover .el-icon {
  color: #fff;
}

/* 表格行样式 */
:deep(.el-table__row) {
  height: 55px;  /* 表示表格行的高度为55px */
  transition: background-color 0.3s;   /* 表示表格行在鼠标悬停时，背景颜色会逐渐变化 */
}

:deep(.el-table__row:hover) {
  background-color: var(--el-color-primary-light-9) !important;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

/* 排序样式 */
:deep(.el-table .caret-wrapper) {
  height: 20px;
  overflow: visible;
}

:deep(.el-table .sort-caret.ascending) {
  border-bottom-color: #c0c4cc;
}

:deep(.el-table .sort-caret.descending) {
  border-top-color: #c0c4cc;
}

:deep(.el-table .ascending .sort-caret.ascending) {
  border-bottom-color: var(--el-color-primary);
}

:deep(.el-table .descending .sort-caret.descending) {
  border-top-color: var(--el-color-primary);
}

:deep(.el-table .ascending .sort-caret.descending),
:deep(.el-table .descending .sort-caret.ascending) {
  opacity: 0.3;
}

/* 暗黑模式适配 */
:deep(.dark-theme) .search-input .el-input__wrapper {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2), 0 0 0 1px #3d4256 inset;
  background-color: #242736;
}

:deep(.dark-theme) .sort-info {
  background-color: #2a2f42;
  color: #e9eaf0;
}

:deep(.dark-theme .card-with-hover:hover) {
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
}
</style>